<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Go by Example 中文版: 命令行标志</title>
    <link rel=stylesheet href="site.css">
  </head>
  <script>
      onkeydown = (e) => {
          
          if (e.key == "ArrowLeft") {
              window.location.href = 'command-line-arguments';
          }
          
          
          if (e.key == "ArrowRight") {
              window.location.href = 'command-line-subcommands';
          }
          
      }
  </script>
  <body>

    <div class="example" id="command-line-flags">
      <h2><a href="./">Go by Example 中文版</a>: 命令行标志</h2>
      
      <table>
        
        <tr>
          <td class="docs">
            <p><a href="http://en.wikipedia.org/wiki/Command-line_interface#Command-line_option"><em>命令行标志</em></a>
是命令行程序指定选项的常用方式。例如，在 <code>wc -l</code> 中，
这个 <code>-l</code> 就是一个命令行标志。</p>

          </td>
          <td class="code empty leading">
            
          
          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            <a href="http://play.golang.org/p/RIYsWf80CiL"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
          <div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>Go 提供了一个 <code>flag</code> 包，支持基本的命令行标志解析。
我们将用这个包来实现我们的命令行程序示例。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
    <span class="s">&quot;flag&quot;</span>
    <span class="s">&quot;fmt&quot;</span>
<span class="p">)</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>基本的标记声明仅支持字符串、整数和布尔值选项。
这里我们声明一个默认值为 <code>&quot;foo&quot;</code> 的字符串标志 <code>word</code> 并带有一个简短的描述。
这里的 <code>flag.String</code> 函数返回一个字符串指针（不是一个字符串值），
在下面我们会看到是如何使用这个指针的。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">wordPtr</span> <span class="o">:=</span> <span class="nx">flag</span><span class="p">.</span><span class="nx">String</span><span class="p">(</span><span class="s">&quot;word&quot;</span><span class="p">,</span> <span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="s">&quot;a string&quot;</span><span class="p">)</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>使用和声明 <code>word</code> 标志相同的方法来声明 <code>numb</code> 和 <code>fork</code> 标志。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">numbPtr</span> <span class="o">:=</span> <span class="nx">flag</span><span class="p">.</span><span class="nx">Int</span><span class="p">(</span><span class="s">&quot;numb&quot;</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="s">&quot;an int&quot;</span><span class="p">)</span>
    <span class="nx">boolPtr</span> <span class="o">:=</span> <span class="nx">flag</span><span class="p">.</span><span class="nx">Bool</span><span class="p">(</span><span class="s">&quot;fork&quot;</span><span class="p">,</span> <span class="kc">false</span><span class="p">,</span> <span class="s">&quot;a bool&quot;</span><span class="p">)</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>用程序中已有的参数来声明一个标志也是可以的。
注意在标志声明函数中需要使用该参数的指针。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="kd">var</span> <span class="nx">svar</span> <span class="kt">string</span>
    <span class="nx">flag</span><span class="p">.</span><span class="nx">StringVar</span><span class="p">(</span><span class="o">&amp;</span><span class="nx">svar</span><span class="p">,</span> <span class="s">&quot;svar&quot;</span><span class="p">,</span> <span class="s">&quot;bar&quot;</span><span class="p">,</span> <span class="s">&quot;a string var&quot;</span><span class="p">)</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>所有标志都声明完成以后，调用 <code>flag.Parse()</code> 来执行命令行解析。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">flag</span><span class="p">.</span><span class="nx">Parse</span><span class="p">()</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>这里我们将仅输出解析的选项以及后面的位置参数。
注意，我们需要使用类似 <code>*wordPtr</code> 这样的语法来对指针解引用，
从而得到选项真正的值。</p>

          </td>
          <td class="code">
            
          <div class="highlight"><pre>    <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;word:&quot;</span><span class="p">,</span> <span class="o">*</span><span class="nx">wordPtr</span><span class="p">)</span>
    <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;numb:&quot;</span><span class="p">,</span> <span class="o">*</span><span class="nx">numbPtr</span><span class="p">)</span>
    <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;fork:&quot;</span><span class="p">,</span> <span class="o">*</span><span class="nx">boolPtr</span><span class="p">)</span>
    <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;svar:&quot;</span><span class="p">,</span> <span class="nx">svar</span><span class="p">)</span>
    <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;tail:&quot;</span><span class="p">,</span> <span class="nx">flag</span><span class="p">.</span><span class="nx">Args</span><span class="p">())</span>
<span class="p">}</span>
</pre></div>

          </td>
        </tr>
        
      </table>
      
      <table>
        
        <tr>
          <td class="docs">
            <p>测试这个程序前，最好将这个程序编译成二进制文件，然后再运行这个程序。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="gp">$</span> go build command-line-flags.go
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>首先以给所有标志赋值的方式，尝试运行构建的程序。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="gp">$</span> ./command-line-flags -word<span class="o">=</span>opt -numb<span class="o">=</span><span class="m">7</span> -fork -svar<span class="o">=</span>flag
<span class="go">word: opt</span>
<span class="go">numb: 7</span>
<span class="go">fork: true</span>
<span class="go">svar: flag</span>
<span class="go">tail: []</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>注意，如果你省略一个标志，那么这个标志的值自动的设定为他的默认值。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="gp">$</span> ./command-line-flags -word<span class="o">=</span>opt
<span class="go">word: opt</span>
<span class="go">numb: 42</span>
<span class="go">fork: false</span>
<span class="go">svar: bar</span>
<span class="go">tail: []</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>尾随的位置参数可以出现在任何标志后面。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="gp">$</span> ./command-line-flags -word<span class="o">=</span>opt a1 a2 a3
<span class="go">word: opt</span>
<span class="go">...</span>
<span class="go">tail: [a1 a2 a3]</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>注意，<code>flag</code> 包需要所有的标志出现位置参数之前（否则，这个标志将会被解析为位置参数）。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="gp">$</span> ./command-line-flags -word<span class="o">=</span>opt a1 a2 a3 -numb<span class="o">=</span>7
<span class="go">word: opt</span>
<span class="go">numb: 42</span>
<span class="go">fork: false</span>
<span class="go">svar: bar</span>
<span class="go">tail: [a1 a2 a3 -numb=7]</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>使用 <code>-h</code> 或者 <code>--help</code> 标志来得到自动生成的这个命令行程序的帮助文本。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="gp">$</span> ./command-line-flags -h
<span class="go">Usage of ./command-line-flags:</span>
<span class="go">  -fork=false: a bool</span>
<span class="go">  -numb=42: an int</span>
<span class="go">  -svar=&quot;bar&quot;: a string var</span>
<span class="go">  -word=&quot;foo&quot;: a string</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>如果你提供了一个没有使用 <code>flag</code> 包声明的标志，
程序会输出一个错误信息，并再次显示帮助文本。</p>

          </td>
          <td class="code">
            
          <div class="highlight"><pre><span class="gp">$</span> ./command-line-flags -wat
<span class="go">flag provided but not defined: -wat</span>
<span class="go">Usage of ./command-line-flags:</span>
<span class="go">...</span>
</pre></div>

          </td>
        </tr>
        
      </table>
      
      
      <p class="next">
        下一个例子: <a href="command-line-subcommands">命令行子命令</a>
      </p>
      
      <p class="footer">
        <a href="https://twitter.com/mmcgrana">@mmcgrana</a> 编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>      </p>
      </p>
    </div>
    <script>
      var codeLines = [];
      codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A    \"flag\"\u000A    \"fmt\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push('    wordPtr := flag.String(\"word\", \"foo\", \"a string\")\u000A');codeLines.push('    numbPtr := flag.Int(\"numb\", 42, \"an int\")\u000A    boolPtr := flag.Bool(\"fork\", false, \"a bool\")\u000A');codeLines.push('    var svar string\u000A    flag.StringVar(&svar, \"svar\", \"bar\", \"a string var\")\u000A');codeLines.push('    flag.Parse()\u000A');codeLines.push('    fmt.Println(\"word:\", *wordPtr)\u000A    fmt.Println(\"numb:\", *numbPtr)\u000A    fmt.Println(\"fork:\", *boolPtr)\u000A    fmt.Println(\"svar:\", svar)\u000A    fmt.Println(\"tail:\", flag.Args())\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');
    </script>
    <script src="site.js" async></script>
  </body>
</html>
